﻿/**************************************************************
 * Copyright gt1987. All rights reserved.
 * 
 * Author: guitao(guitao@eastmoney.com) 
 * Create Date: 2020/4/14 15:56:44
 * Description: DateTimeHelper
 *          
 * Revision History:
 *      Date         Author               Description
 *              
***************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace gt.ShortMessage.Utils
{
    public class DateTimeHelper
    {
        /// <summary>
        /// datetime->时间戳
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static long ToTimeStamp(DateTime source)
        {
            var startTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Unspecified).ToLocalTime();
            return (long)(source - startTime).TotalSeconds;
        }
        /// <summary>
        /// 时间戳->datetime
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(long timeStamp)
        {
            var startTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Unspecified).ToLocalTime();
            var newTime = startTime.AddSeconds(timeStamp);
            return newTime.ToLocalTime();
        }
    }
}
